package one.chapter_1.bag;

import java.util.Iterator;

public class LinkedBag<Item> implements Bag<Item> {

    private Node first;
    private int size;

    public Iterator<Item> iterator() {
        return new Iter();
    }

    @Override
    public void add(Item item) {
        /**
         * 从头插入
         */
        Node node = new Node();
        node.item = item;

        Node oldFirst = first;
        node.next = oldFirst;
        first = node;

        size++;
    }



    public boolean isEmpty() {
        return first == null;
    }

    public int size() {
        return size;
    }


    private class Node {
        Item item;
        Node next;
    }

    private class Iter implements Iterator<Item> {

        private int pos;
        private Node nextNode = first;

        public boolean hasNext() {
            return nextNode != null;
        }

        public Item next() {

            Item item = nextNode.item;
            nextNode = nextNode.next;
            return item;
        }

        public void remove() {

        }
    }


}
